package com.xn.app.muying.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xn.app.muying.R;
import com.xn.app.muying.util.BaseActivity;
import com.xn.app.muying.util.MyImageLoader;
import com.xn.app.muying.util.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 语音解答
 * Created by gaoge on 2017/8/11.
 */

public class VoiceSolutionActivity extends BaseActivity implements View.OnClickListener {

    //标题栏
    private TextView title_name = null;
    //返回键
    private ImageView img_back = null;
    //布局listview
    private ListView voice_listview = null;
    //搜索框
    private EditText seek_edittext = null;
    //妇产科搜索
    private LinearLayout woman_layout = null;
    //新生儿科搜索
    private LinearLayout baby_layout = null;
    //儿科搜索
    private LinearLayout children_layout = null;
    //陕西省人民医院
    private ImageView hospital1 = null;
    //西安交通大学第一附属医院
    private ImageView hospital2 = null;
    //西安市儿童医院
    private ImageView hospital3 = null;
    //搜索按钮
    private LinearLayout seek_layout = null;
    //适配器
    private MyAdpter myAdpter = null;
    //图片加载
    private ImageLoader imageLoader = null;
    private DisplayImageOptions options = null;
    //数据保存的集合
    private ArrayList<HashMap<String, String>> map = new ArrayList<>();
    //搜索的分类
    private String type = "";
    //搜索的内容
    private String data = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //加载布局
        setContentView(R.layout.voice_solution_activity);
        //初始化组件
        init();
    }

    /**
     * 初始化组件
     */
    private void init() {
        //加载并缓存图片
        imageLoader = MyImageLoader.getImageLoader(this);
        // DisplayImageOptions对象是对下载好以后的图片，在显示出来的时候做的设置
        options = new DisplayImageOptions.Builder().showImageOnLoading(0) // 在下载期间显示的图片
                .showImageForEmptyUri(R.mipmap.load_failed) // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.mipmap.load_failed) // 设置图片加载或解码过程中发生错误显示的图片
                .bitmapConfig(Bitmap.Config.RGB_565) // 防止内存溢出的，图片太多就这这个
                .cacheInMemory(true).cacheOnDisc(true).build();
        //初始化组件
        title_name = (TextView) findViewById(R.id.title_name);
        img_back = (ImageView) findViewById(R.id.img_back);
        voice_listview = (ListView) findViewById(R.id.voice_listview);
        View inflate = LayoutInflater.from(getApplicationContext()).inflate(R.layout.voice_activity_top, null);
        seek_edittext = (EditText) inflate.findViewById(R.id.seek_edittext);
        seek_edittext.setSelection(seek_edittext.length());
        woman_layout = (LinearLayout) inflate.findViewById(R.id.woman_layout);
        baby_layout = (LinearLayout) inflate.findViewById(R.id.baby_layout);
        children_layout = (LinearLayout) inflate.findViewById(R.id.children_layout);
        hospital1 = (ImageView) inflate.findViewById(R.id.hospital1);
        hospital2 = (ImageView) inflate.findViewById(R.id.hospital2);
        hospital3 = (ImageView) inflate.findViewById(R.id.hospital3);
        seek_layout = (LinearLayout) inflate.findViewById(R.id.seek_layout);
        //设置标题
        title_name.setText("语音解答");
        //添加单击事件
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        seek_edittext.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容
                } else {
                    // 此处为失去焦点时的处理内容
                }
                voice_listview.setSelection(0);
            }
        });
        woman_layout.setOnClickListener(this);
        baby_layout.setOnClickListener(this);
        children_layout.setOnClickListener(this);
        hospital1.setOnClickListener(this);
        hospital2.setOnClickListener(this);
        hospital3.setOnClickListener(this);
        seek_layout.setOnClickListener(this);
        //给listview添加头
        voice_listview.addHeaderView(inflate);
        //listView添加适配器
        myAdpter = new MyAdpter(this, map, imageLoader, options);
        voice_listview.setAdapter(myAdpter);
    }

    /**
     * 单击事件
     *
     * @param v 控件
     */
    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            //妇产科
            case R.id.woman_layout:
                type = "departmentName";
                data = "妇产科";
                break;
            //新生儿科
            case R.id.baby_layout:
                type = "departmentName";
                data = "新生儿科";
                break;
            //儿科
            case R.id.children_layout:
                type = "departmentName";
                data = "儿科";
                break;
            //陕西省人民医院
            case R.id.hospital1:
                type = "hospitalName";
                data = "陕西省人民医院";
                break;
            //西安交通大学第一附属医院
            case R.id.hospital2:
                type = "hospitalName";
                data = "西安交通大学第一附属医院";
                break;
            //西安市儿童医院
            case R.id.hospital3:
                type = "hospitalName";
                data = "西安市儿童医院";
                break;
            //搜索按钮
            case R.id.seek_layout:
                type = "title";
                data = seek_edittext.getText().toString();
                break;
        }
        //关闭键盘
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive() && getCurrentFocus() != null) {
            if (getCurrentFocus().getWindowToken() != null) {
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
        //提交
        setSearch(type, data);
    }

    /**
     * 搜索内容
     *
     * @param type 分类
     * @param data 内容
     */
    public void setSearch(String type, String data) {
        if (data != null && !data.equals("")) {
            Intent intent = new Intent(this, VoiceSolutionListActivity.class);
            intent.putExtra("data", data);
            intent.putExtra("type", type);
            startActivity(intent);
        } else {
            ToastUtil.showToast(this, "请输入关键词查询");
        }

    }

    /**
     * 列表适配器
     */
    class MyAdpter extends BaseAdapter {
        LayoutInflater lf = null;
        ArrayList<HashMap<String, String>> getData = new ArrayList<HashMap<String, String>>();
        ViewHolder viewHolder = null;
        private ImageLoader imageLoader = null;
        private DisplayImageOptions options = null;

        public MyAdpter(Context context,
                        ArrayList<HashMap<String, String>> getData, ImageLoader loader,
                        DisplayImageOptions options) {
            lf = LayoutInflater.from(context);
            this.imageLoader = loader;
            this.options = options;
            this.getData = getData;
        }

        @Override
        public int getCount() {
            return getData.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @SuppressLint("InflateParams")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = lf.inflate(R.layout.education_list_item, null);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            return convertView;
        }
    }

    /**
     * 包装类
     */
    private static class ViewHolder {
        private ImageView imageview = null;
        private TextView textview_title = null;
        private TextView textview_content = null;
    }
}
